package com.jinlejia.erp.ui.mine.transfer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.common.WrapContentLinearLayoutManager;
import com.jinlejia.erp.databinding.ActivityTransferApplyMoreListBinding;
import com.jinlejia.erp.ui.mine.adapter.TransFerOneListAdapter;
import com.weight.CustomToast;

import java.util.ArrayList;
import java.util.Map;

public class TransferApplyMoreListActivity extends AppCompatActivity {
    private ActivityTransferApplyMoreListBinding binding;
    private ArrayList<Map<String, Object>> mapArrayList;//数据集合
    private ArrayList<Map<String, Object>> mapArrayList2 = new ArrayList<>();//数据集合2
    private ArrayList<Map<String, Object>> mapArrayList3 = new ArrayList<>();//数据集合3
    private TransFerOneListAdapter transFerOneListAdapter1;
    private TransFerOneListAdapter transFerOneListAdapte2;
    private TransFerOneListAdapter transFerOneListAdapter3;
    private String reason1 = null, reason2 = null, reason3 = null, reasonAll = null; //异动原因选择结果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_transfer_apply_more_list);
        binding.setEvent(this);
        Bundle bundle = getIntent().getExtras();
        mapArrayList = (ArrayList<Map<String, Object>>) bundle.getSerializable("reasonsForChangeList");
        initView();
    }

    private void initView() {
        binding.tvTitle.setText(getIntent().getStringExtra("title"));
        binding.recycler1.setLayoutManager(new WrapContentLinearLayoutManager(this));
        binding.recycler2.setLayoutManager(new WrapContentLinearLayoutManager(this));
        binding.recycler3.setLayoutManager(new WrapContentLinearLayoutManager(this));

        transFerOneListAdapter1 = new TransFerOneListAdapter(this, mapArrayList);
        transFerOneListAdapte2 = new TransFerOneListAdapter(this, new ArrayList<>());
        transFerOneListAdapter3 = new TransFerOneListAdapter(this, new ArrayList<>());

        binding.recycler1.setAdapter(transFerOneListAdapter1);
        binding.recycler2.setAdapter(transFerOneListAdapte2);
        binding.recycler3.setAdapter(transFerOneListAdapter3);

        //region 第一列点击事件
        transFerOneListAdapter1.setOnItemSelectedListener(map -> {
            mapArrayList2 = (ArrayList<Map<String, Object>>) map.get("list");
            binding.recycler3.setVisibility(View.GONE);
            reason1 = String.valueOf(map.get("name"));
            reason2 = "";
            reason3 = "";
            if (mapArrayList2.size() > 0) {
                binding.recycler2.setVisibility(View.VISIBLE);
                transFerOneListAdapte2.cur_position = -1;
                transFerOneListAdapte2.setData(mapArrayList2);
            } else {
                binding.recycler2.setVisibility(View.GONE);
                mapArrayList2.clear();
            }
        });
        transFerOneListAdapter1.setOnItemRemoveListener(map -> {
        });
        //endregion
        //region 第二列点击事件
        transFerOneListAdapte2.setOnItemSelectedListener(map -> {
            mapArrayList3 = (ArrayList<Map<String, Object>>) map.get("list");
            reason2 = String.valueOf(map.get("name"));
            reason3 = "";
            if (mapArrayList3.size() > 0) {
                binding.recycler3.setVisibility(View.VISIBLE);
                transFerOneListAdapter3.cur_position = -1;
                transFerOneListAdapter3.setData(mapArrayList3);
            } else {
                binding.recycler3.setVisibility(View.GONE);
                mapArrayList3.clear();
            }
        });

        transFerOneListAdapte2.setOnItemRemoveListener(map -> {
        });
        //endregion


        //region 第三列点击事件
        transFerOneListAdapter3.setOnItemSelectedListener(map -> {
            reason3 = String.valueOf(map.get("name"));
        });

        transFerOneListAdapter3.setOnItemRemoveListener(map -> {
        });
        //endregion
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_save:
                if (binding.recycler1.getVisibility() == View.VISIBLE && TextUtils.isEmpty(reason1)) {
                    CustomToast.showToast(this, "请选择", 1500);
                } else if (binding.recycler2.getVisibility() == View.VISIBLE && TextUtils.isEmpty(reason2)) {
                    CustomToast.showToast(this, "请选择到第二级", 1500);
                } else if (binding.recycler3.getVisibility() == View.VISIBLE && TextUtils.isEmpty(reason3)) {
                    CustomToast.showToast(this, "请选择到第三级", 1500);
                } else {
                    if (binding.recycler3.getVisibility() == View.VISIBLE) {
                        reasonAll = reason1.concat(",").concat(reason2).concat(",").concat(reason3);
                    } else if (binding.recycler2.getVisibility() == View.VISIBLE) {
                        reasonAll = reason1.concat(",").concat(reason2);
                    } else {
                        reasonAll = reason1;
                    }
                    if (reasonAll.contains("其他")||reasonAll.contains("校园招聘")){
                        CustomToast.showToast(this, "请填写详细原因", 1500);
                    }else{
                        Intent intent = new Intent();
                        intent.putExtra("reason", reasonAll);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }
                break;
        }
    }

    //获取列表输入框内容回调的方法
    public void saveEditStringData(String str) {
        if (binding.recycler2.getVisibility() == View.GONE) {
            reason1 = str;
        } else if (binding.recycler3.getVisibility() == View.GONE) {
            reason2 = str;
        } else {
            reason3 = str;
        }

    }
}